<?php
//$list_fields = get_fields('list', $module_fields);
$list_fields = get_list_order($module_fields);
$search_fields = get_fields('search', $module_fields);
$search_fields = get_names('name', $search_fields);

//			print_r($search_fields);
//			print_r($module_fields);
//		die;

$module_name = $module_name;
$module_name_small = $module_name_small;
$total_fields = count($list_fields) + 2;

$pk_field = array();
foreach($module_fields as $each_field) {
	if($each_field['pk']) {
		$pk_field = $each_field;
	}
}

if(!empty($pk_field['name']))
	$primary_key = $pk_field['name'];
else
	$primary_key = $module_name_small."_id";

ob_start();
foreach ($list_fields as $each_field) {
	echo "\r" . '			  <td class="left"><?php if ($sort == \'' . $each_field['name'] . '\') { ?>' . "\n" .
	'				<a href="<?php echo $sort_' . $each_field['name'] . '; ?>" class="<?php echo strtolower($order); ?>"><?php echo $column_' . $each_field['name'] . '; ?></a>' . "\n" .
	'				<?php } else { ?>' . "\n" .
	'				<a href="<?php echo $sort_' . $each_field['name'] . '; ?>"><?php echo $column_' . $each_field['name'] . '; ?></a>' . "\n" .
	'				<?php } ?></td>' . "\n";
}
$list_column_fields = ob_get_clean();

ob_start();
foreach ($list_fields as $each_field) {
	echo "\r" . '			  <td class="left"><?php echo $' . $module_name_small . '[\'' . $each_field['name'] . '\']; ?></td>';
}
$list_data_fields = ob_get_clean();

ob_start();
if(count($search_fields) > 0) {
	echo "\r" . '			  <tr class="filter">' . "\n";	
	echo '				<td></td>' . "\n";
	foreach ($list_fields as $each_field) {	
		if(in_array($each_field['name'], $search_fields)) {
			switch($each_field['type']) {
				case 'date_added':
				case 'date_modified':
				case 'datetime':
				case 'date':
				echo '				<td><input type="text" name="filter_' . $each_field['name'] . '" value="<?php echo $filter_' . $each_field['name'] . '; ?>" size="12" class="date" /></td>' . "\n";
				
				break;
				case 'status':
				case 'select':
					$field_name = $each_field['name'];
					if(preg_match('@_id@', $each_field['name'])) {
						$each_field['name'] = str_replace('_id', '', $each_field['name']);	
					}
				echo '				<td><select name="filter_' . $field_name . '">' . "\n" .
						'				<option value="*"></option>' . "\n" .
						'				<?php foreach ($' . $each_field['name'] . 's as $each_' . $each_field['name'] . ') { ?>' . "\n" .
						'				<?php if ($each_' . $each_field['name'] . '[\'' . $each_field['name'] . '_id\'] == $filter_' . $field_name . ') { ?>' . "\n" .
						'				<option value="<?php echo $each_' . $each_field['name'] . '[\'' . $each_field['name'] . '_id\']; ?>" selected="selected"><?php echo $each_' . $each_field['name'] . '[\'name\']; ?></option>' . "\n" .
						'				<?php } else { ?>' . "\n" .
						'				<option value="<?php echo $each_' . $each_field['name'] . '[\'' . $each_field['name'] . '_id\']; ?>"><?php echo $each_' . $each_field['name'] . '[\'name\']; ?></option>' . "\n" .
						'				<?php } ?>' . "\n" .
						'				<?php } ?>' . "\n" .
						'				</select></td>' . "\n";
				break;
				default:
					echo '				<td><input type="text" name="filter_' . $each_field['name'] . '" value="<?php echo $filter_' . $each_field['name'] . '; ?>" size="12" /></td>' . "\n";
				break;	
			}
		} else {
					echo '				<td></td>' . "\n";	
		}			
	}
	
	echo '				<td align="right"><a onclick="filter();" class="button"><span><?php echo $button_filter; ?></span></a></td>' . "\n" .
		 '			  </tr>'. "\n";
}
$filter_fields = ob_get_clean();

ob_start();
if(count($search_fields) > 0) {
	echo "\r" . '<script type="text/javascript"><!--' . "\n" .
			'function filter() {' . "\n" .
			'	url = \'index.php?route=' . $module_path . '&token=<?php echo $token; ?>\';'. "\n\n";

	foreach ($list_fields as $each_field) {				
		if(in_array($each_field['name'], $search_fields)) {
			switch($each_field['type']) {			
				case 'status':
				case 'select':
					echo '	var filter_' . $each_field['name'] . ' = $(\'select[name=\\\'filter_' . $each_field['name'] . '\\\']\').attr(\'value\');' . "\n" .
			 		'	if (filter_' . $each_field['name'] . ' != \'*\') {' . "\n" .
			 		'		url += \'&filter_' . $each_field['name'] . '=\' + encodeURIComponent(filter_' . $each_field['name'] . ');' . "\n" .
			 		'	}'. "\n\n";			
				break;
				default:
					echo '	var filter_' . $each_field['name'] . ' = $(\'input[name=\\\'filter_' . $each_field['name'] . '\\\']\').attr(\'value\');' . "\n" .
			  		'	if (filter_' . $each_field['name'] . ') {' . "\n" .
			  		'		url += \'&filter_' . $each_field['name'] . '=\' + encodeURIComponent(filter_' . $each_field['name'] . ');' . "\n" .
			  		'	}'. "\n\n";
				break;	
			}
		}
	}					
	
	echo '		location = url;' . "\n" .
		 '}' . "\n" .
		 '//--></script>' . "\n" .
		 '<script type="text/javascript"><!--' . "\n" .
		 '	$(document).ready(function() {' . "\n" .
		 '		$(\'.date\').datepicker({dateFormat: \'yy-mm-dd\'});' . "\n" .
		 '	});' . "\n" .
		 '//--></script>' . "\n" .
		 '<script type="text/javascript"><!--' . "\n" .
		 '	$(\'#form input\').keydown(function(e) {' . "\n" .
		 '		if (e.keyCode == 13) {' . "\n" .
		 '			filter();' . "\n" .
		 '		}' . "\n" .
		 '	});' . "\n" .
		 '//--></script>'. "\n";
}
$java_scripts = ob_get_clean();

$maches_arr = array (
	'/@module_name@/',
	'/@module_name_small@/',
	'/@total_fields@/',
	'/@list_column_fields@/',
	'/@primary_key@/',
	'/@list_data_fields@/',
	'/@filter_fields@/',
	'/@java_scripts@/',
	
);

$replace_arr = array (
	$module_name,
	$module_name_small,
	$total_fields,
	$list_column_fields,
	$primary_key,
	$list_data_fields,
	$filter_fields,
	$java_scripts,
	
);

//	preg_match_all('/@([^@]+)@/', $list_file_contents, $maches);
//	sort($maches['1']);
//	print_r(array_unique($maches['1']));

$list_file_contents = preg_replace($maches_arr, $replace_arr, $list_file_contents);
?>